#include "GameRecord.h"

#include <QFile>
#include <QTextStream>


GameRecord::GameRecord(void)
    :name(""), file(NULL), stream(NULL)
{
}


GameRecord::~GameRecord(void)
{
    destroy();
}

void GameRecord::Save( QString fileName, const QList<int> &record )
{
    QFile file(fileName);

    if (file.open(QIODevice::WriteOnly))
    {
        QTextStream out(&file);

        for (auto iter = record.begin(); iter != record.end(); ++iter)
        {
            out << *iter << endl;
        }
    }
}

void GameRecord::destroy()
{
    if (file != NULL)
    {
        delete stream;
        delete file;

        file = NULL;
        stream = NULL;
    }    
}

bool GameRecord::Load( QString fileName )
{
    destroy();

    file = new QFile(fileName);
    if (!file->open(QIODevice::ReadOnly))
    {
        destroy();
        return false;
    }

    stream = new QTextStream(file);
    return true;
}

int GameRecord::NextElement()
{
    int ret = INT_MAX;
    if (file != NULL && !stream->atEnd())
    {
        QString str = stream->readLine();

        if (!str.isEmpty())
        {
            ret = str.toInt();
        }
    }

    return ret;
}
